/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Main.java
 *
 * Created on 12/02/2014, 17:32:13
 */
package semanticrawler;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import utfpr.cp.crawler.ControllerCrawler;
import utfpr.cp.crawler.CrawlerStats;
import utfpr.cp.extractor.ParseHTMLTags;
import utfpr.cp.model.URLTargetDAO;
import utfpr.cp.parser.PredicateFactory;

/**
 *
 * @author COINF-PROFESSOR-07
 */
public class Main extends javax.swing.JFrame {

    /**
     * Creates new form Main
     */
    public Main() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        openFile = new javax.swing.JFileChooser();
        jButton1 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        modo = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        labelPagina = new javax.swing.JLabel();
        labelUrl = new javax.swing.JLabel();
        labelSites = new javax.swing.JLabel();
        labelTags = new javax.swing.JLabel();
        labelAttr = new javax.swing.JLabel();
        labelHeader = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        labelParser = new javax.swing.JLabel();
        sliderThreads = new javax.swing.JSlider();
        jLabel9 = new javax.swing.JLabel();
        labelThreads = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        textAreaConsole = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaSitesManual = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        ligarServidorAutomatico = new javax.swing.JMenuItem();
        ligarPorEtapa = new javax.swing.JMenuItem();
        ligarSErvidorManual = new javax.swing.JMenuItem();
        ligarServidorTeste = new javax.swing.JMenuItem();
        importarSites = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        openFile.setDialogTitle("Lista de sites");
        openFile.setToolTipText("Selecione o arquivo contendo os sites que deverão ser importados");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jButton1.setText("Sair");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        modo.setSelected(true);
        modo.setText("Utilizar modo restrito (baixar conteúdo somente dos sites pré-definidos)");
        modo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modoActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Resumo dos dados"));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("URLs para baixar:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Páginas coletadas:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Sites cadastrados:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Tags extraídas:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setText("Attributos extraídos:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel7.setText("Headers HTTP:");

        labelPagina.setText("[pagina]");

        labelUrl.setText("[url]");

        labelSites.setText("[sites]");

        labelTags.setText("[tags]");

        labelAttr.setText("[attr]");

        labelHeader.setText("[header]");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel8.setText("Páginas para parsear:");

        labelParser.setText("[parser]");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(labelSites)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelUrl)
                            .addComponent(labelPagina)
                            .addComponent(labelParser))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelTags)
                            .addComponent(labelAttr)
                            .addComponent(labelHeader))
                        .addGap(70, 70, 70))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(labelPagina, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(9, 9, 9)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(labelUrl))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(labelParser)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(labelTags))
                        .addGap(9, 9, 9)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(labelAttr))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(labelHeader))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(labelSites))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        sliderThreads.setMaximum(50);
        sliderThreads.setMinimum(1);
        sliderThreads.setValue(8);
        sliderThreads.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderThreadsStateChanged(evt);
            }
        });

        jLabel9.setText("Número de Threads:");

        labelThreads.setText("8");

        textAreaConsole.setEditable(false);
        textAreaConsole.setBackground(new java.awt.Color(240, 240, 240));
        textAreaConsole.setColumns(20);
        textAreaConsole.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        textAreaConsole.setLineWrap(true);
        textAreaConsole.setRows(5);
        jScrollPane2.setViewportView(textAreaConsole);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(modo)
                .addGap(0, 182, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sliderThreads, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelThreads)
                .addContainerGap(233, Short.MAX_VALUE))
            .addComponent(jScrollPane2)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(modo)
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sliderThreads, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(labelThreads))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jTabbedPane1.addTab("Geral", jPanel1);

        listaSitesManual.setColumns(20);
        listaSitesManual.setRows(5);
        jScrollPane1.setViewportView(listaSitesManual);

        jLabel1.setText("Informe um domínio por linha");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 393, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Manual", jPanel2);

        jMenu1.setText("Crawler");

        jMenu3.setText("Iniciar Servidor");

        ligarServidorAutomatico.setText("Automático");
        ligarServidorAutomatico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ligarServidorAutomaticoActionPerformed(evt);
            }
        });
        jMenu3.add(ligarServidorAutomatico);

        ligarPorEtapa.setText("Site por site");
        ligarPorEtapa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ligarPorEtapaActionPerformed(evt);
            }
        });
        jMenu3.add(ligarPorEtapa);

        ligarSErvidorManual.setText("Manual");
        ligarSErvidorManual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ligarSErvidorManualActionPerformed(evt);
            }
        });
        jMenu3.add(ligarSErvidorManual);

        ligarServidorTeste.setText("Teste");
        ligarServidorTeste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ligarServidorTesteActionPerformed(evt);
            }
        });
        jMenu3.add(ligarServidorTeste);

        jMenu1.add(jMenu3);

        importarSites.setText("Importar Sites");
        importarSites.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importarSitesActionPerformed(evt);
            }
        });
        jMenu1.add(importarSites);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Analise");

        jMenuItem1.setText("Parsear HTML");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jTabbedPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        setVisible(false);
        System.exit(0);
    }//GEN-LAST:event_jButton1ActionPerformed

    ControllerCrawler control;
    boolean servidorPorEtata = false;
    private boolean servidorLigado() {
        return servidorPorEtata || (control != null && !control.isFinished());
    }
    
    private void ligarServidorAutomaticoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ligarServidorAutomaticoActionPerformed
        if (servidorLigado()) {
            JOptionPane.showMessageDialog(this, "Existe um controle ativo no momento!");
            return;
        }
        textAreaConsole.setText("Lingando o servidor automático...");
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    control = RunCrawler.ligarServidorAutomatizado(modo.isSelected());
                } catch (Exception ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        Thread thread = new Thread(myRunnable);
        thread.start();
    }//GEN-LAST:event_ligarServidorAutomaticoActionPerformed

    private void ligarSErvidorManualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ligarSErvidorManualActionPerformed
        if (servidorLigado()) {
            JOptionPane.showMessageDialog(this, "Existe um controle ativo no momento!");
            return;
        }
        textAreaConsole.setText("Lingando o servidor manual...");
        final String[] sites = listaSitesManual.getText().split("\n");
        for (String site : sites) {
            System.out.println("Loading site "+site);
        }
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    control = RunCrawler.ligarServidorManual(modo.isSelected(), sites);
                } catch (Exception ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        Thread thread = new Thread(myRunnable);
        thread.start();
    }//GEN-LAST:event_ligarSErvidorManualActionPerformed

    private void ligarServidorTesteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ligarServidorTesteActionPerformed
        if (servidorLigado()) {
            JOptionPane.showMessageDialog(this, "Existe um controle ativo no momento!");
            return;
        }
        textAreaConsole.setText("Lingando o servidor de teste...");
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    control = RunCrawler.ligarServidorTeste(modo.isSelected());
                } catch (Exception ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        Thread thread = new Thread(myRunnable);
        thread.start();
    }//GEN-LAST:event_ligarServidorTesteActionPerformed

    private void modoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_modoActionPerformed

    private void importarSitesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importarSitesActionPerformed
        int returnVal = openFile.showSaveDialog(Main.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = openFile.getSelectedFile();
            URLTargetDAO dao = URLTargetDAO.factory();
            try {
                BufferedReader leitor = new BufferedReader(new FileReader(file));
                while (leitor.ready()) {
                    dao.save(leitor.readLine());
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Falha ao abrir o arquivo", ex);
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Falha ao ler o arquivo", ex);
            } 
        }
    }//GEN-LAST:event_importarSitesActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        final ParseHTMLTags parser = new ParseHTMLTags();
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                boolean running = true;
                while (running) {
                    try {
                        parser.run();
                        running = false;
                    } catch (Exception ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        };
        Thread thread = new Thread(myRunnable);
        thread.start();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        if (labelTags.getText().equals("[tags]")) {
            labelTags.setText("loading...");
            labelAttr.setText("loading...");
            labelHeader.setText("loading...");
            labelPagina.setText("loading...");
            labelSites.setText("loading...");
            labelUrl.setText("loading...");
            labelParser.setText("loading...");

            Runnable myRunnable = new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            HashMap<String, Long> dados = RunCrawler.getInfoGeral();
                            labelTags.setText(dados.get("tags").toString());
                            labelAttr.setText(dados.get("attr").toString());
                            labelHeader.setText(dados.get("header").toString());
                            labelPagina.setText(dados.get("pagina").toString());
                            labelUrl.setText(dados.get("url").toString());
                            labelSites.setText(dados.get("sites").toString());
                            labelParser.setText(dados.get("parser").toString());

                            if (servidorLigado()) { 
                                if (control != null) {
                                    StringBuilder sb = new StringBuilder();
                                    sb.append(control.getResumoAtual());
                                    for (CrawlerStats st : control.getStatisticsList()) {
                                        sb.append("\n").append(st);
                                    }
                                    textAreaConsole.setText(sb.toString());
                                }
                                else {
                                    //Etapa por etapa, não faz nada console passado para o thread que genrecia os servidores
                                }
                            }
                            else {
                                textAreaConsole.setText("Servidor desligado!");
                            }
                            Thread.sleep(120000);
                        } catch (Exception ex) {
                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            };
            Thread thread = new Thread(myRunnable);
            thread.start();
        }
    }//GEN-LAST:event_formWindowActivated

    private void sliderThreadsStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderThreadsStateChanged
        labelThreads.setText(String.valueOf(sliderThreads.getValue()));
        RunCrawler.NUMERO_THREADS = sliderThreads.getValue();
    }//GEN-LAST:event_sliderThreadsStateChanged

    private void ligarPorEtapaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ligarPorEtapaActionPerformed
        if (servidorLigado()) {
            JOptionPane.showMessageDialog(this, "Existe um controle ativo no momento!");
            return;
        }
        servidorPorEtata = true;
        textAreaConsole.setText("Ligando servidor site por site...");
        try {
            RunCrawler.ligarServidorPorEtapa(modo.isSelected(), textAreaConsole);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ligarPorEtapaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        RunCrawler.NUMERO_THREADS = 8;

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem importarSites;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel labelAttr;
    private javax.swing.JLabel labelHeader;
    private javax.swing.JLabel labelPagina;
    private javax.swing.JLabel labelParser;
    private javax.swing.JLabel labelSites;
    private javax.swing.JLabel labelTags;
    private javax.swing.JLabel labelThreads;
    private javax.swing.JLabel labelUrl;
    private javax.swing.JMenuItem ligarPorEtapa;
    private javax.swing.JMenuItem ligarSErvidorManual;
    private javax.swing.JMenuItem ligarServidorAutomatico;
    private javax.swing.JMenuItem ligarServidorTeste;
    private javax.swing.JTextArea listaSitesManual;
    private javax.swing.JCheckBox modo;
    private javax.swing.JFileChooser openFile;
    private javax.swing.JSlider sliderThreads;
    private javax.swing.JTextArea textAreaConsole;
    // End of variables declaration//GEN-END:variables
}
